package com.ak;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

import java.util.Properties;
import java.util.concurrent.Executor;

public class demo2 {

    public static void main(String[] args) throws NacosException {
        // 使用nacos 来获取配置
        String serverAddr="47.100.104.187:8848";

        String dataID="nacos-simple-demo1.yml";

        String group="DEFAULT_GROUP";
        String nameSpace="cf6270d3-a5d2-46ac-a36a-051ef8fd0151";
        Properties properties =new Properties();
        properties.put("serverAddr",serverAddr);
        properties.put("namespace",nameSpace);
        ConfigService service=NacosFactory.createConfigService(properties);
        String config=service.getConfig(dataID,group,5000);
        System.out.println(config);
        // 添加了监听，当配置信息内容发生改变了，就会发生改变
        service.addListener(dataID, group, new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }

            @Override
            public void receiveConfigInfo(String s) {
                System.out.println(s);
            }
        });
        while (true){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
